package com.haoxin.ai_course.modules.course.biz;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haoxin.ai_course.common.utils.HttpJsonResult;
import com.haoxin.ai_course.common.utils.UrlDeal;
import com.haoxin.ai_course.modules.course.entity.AcArticle;
import com.haoxin.ai_course.modules.course.entity.AcArticleCount;
import com.haoxin.ai_course.modules.course.entity.AcCategory;
import com.haoxin.ai_course.modules.course.entity.AcStatisticsArticle;
import com.haoxin.ai_course.modules.course.entity.PO.AcCategoryPO;
import com.haoxin.ai_course.modules.course.entity.PO.StatisticsArticlePO;
import com.haoxin.ai_course.modules.course.entity.VO.AcArticleVO;
import com.haoxin.ai_course.modules.course.entity.VO.AcCategoryVO;
import com.haoxin.ai_course.modules.course.service.AcArticleCountService;
import com.haoxin.ai_course.modules.course.service.AcArticleService;
import com.haoxin.ai_course.modules.course.service.AcCategoryService;
import com.haoxin.ai_course.modules.course.service.AcStatisticsArticleService;
import com.haoxin.ai_course.modules.sys.entity.SysUser;

import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;

/**
 * 答题结果表
 * 
 * @author xingxiangguo
 * @email bugcn@qq.com
 * @date 2022-06-06 09:44:49
 */
@Slf4j
@Component
public class AcCategoryBusiness {
	@Autowired
	 private MapperFacade mapperFacade;
	@Autowired
	AcCategoryService acCategoryService ;
	@Autowired
	AcArticleService acArticleService;
	@Autowired
	AcStatisticsArticleService acStatisticsArticleService;
	@Autowired
	AcArticleCountService acArticleCountService;
	public HttpJsonResult<List<AcCategoryVO>> fullList() { 
		HttpJsonResult<List<AcCategoryVO>> result = new HttpJsonResult<List<AcCategoryVO>>();
		List<AcCategory>list = acCategoryService.list();
		List<AcCategoryVO> voList = mapperFacade.mapAsList(list, AcCategoryVO.class);
		for (AcCategoryVO acCategoryVO : voList) {
			QueryWrapper<AcArticle> qw = new QueryWrapper<AcArticle>();
			qw.lambda().eq(AcArticle::getCategoryId, acCategoryVO.getId());
			qw.lambda().orderByDesc(AcArticle::getId);
			
			List<AcArticle>  acArticleList = acArticleService.list(qw);
			List<AcArticleVO>  acArticleVOList = mapperFacade.mapAsList(acArticleList, AcArticleVO.class);
			
			for (AcArticleVO acArticleVO : acArticleVOList) {
				boolean printLog = false;
				if(acArticleVO.getId() ==43||44== acArticleVO.getId()  ||30==acArticleVO.getId() ) {
					printLog = true;
				}
				
				acArticleVO.setContent(replaceStr(acArticleVO.getContent(),printLog));
			}
			acCategoryVO.setAcrticleVOList(acArticleVOList);
		}
		result.setData(voList);
		return result;
	}
	public HttpJsonResult<Boolean> saveAcCategory(AcCategoryPO acCategoryPO) {
		HttpJsonResult<Boolean> result = new HttpJsonResult<Boolean> ();
		AcCategory acCategory = mapperFacade.map(acCategoryPO, AcCategory.class);
		result.setData(acCategoryService.save(acCategory));
		return result;
	}
	public HttpJsonResult<AcCategoryVO> getById(Integer id) {
		HttpJsonResult<AcCategoryVO>  result = new  HttpJsonResult<AcCategoryVO> ();
		AcCategory acCategory = 	acCategoryService.getById(id);
		result.setData(mapperFacade.map(acCategory, AcCategoryVO.class));
		return result;
	}
	
	private String replaceStr(String content,boolean printLog) {
    	content = UrlDeal.decodeURIComponent(content);
    	if(printLog == true) {
    		log.info("-------替换前-------："+content);
    	}
    	String result = "";
    	HashMap<String,String> map = new HashMap<String,String>();
    	map.put("%23", "#");
    	map.put("%26", "&");
    	map.put("%3B", ";");
    	Set<String> keys = map.keySet();
    	for (String key : keys) {
    		content = content.replaceAll(key, map.get(key));
		}
    	if(printLog == true) {
    		log.info("-------替换后-------："+content);
    	}
    	
    	result = 	UrlDeal.encodeURIComponent(content);
    	return result;
    }
	public HttpJsonResult<Boolean> articleCount(StatisticsArticlePO statisticsArticlePO, SysUser sysUser) {
		HttpJsonResult<Boolean>  result = new HttpJsonResult<Boolean> ();
		AcArticle acArticle = acArticleService.getById(statisticsArticlePO.getArticleId());
		if(acArticle == null) {
			result.setMsg("文章不存在");
			result.setData(false);
			return result;
		}
		QueryWrapper<AcStatisticsArticle> qw = new QueryWrapper<AcStatisticsArticle>();
		qw.lambda().eq(AcStatisticsArticle::getArticleId, statisticsArticlePO.getArticleId());
		qw.lambda().eq(AcStatisticsArticle::getUserId, sysUser.getUserId());
		AcStatisticsArticle acStatisticsArticle =	acStatisticsArticleService.getOne(qw);
		if(acStatisticsArticle == null) {
			acStatisticsArticle = mapperFacade.map(statisticsArticlePO, AcStatisticsArticle.class);
			acStatisticsArticle.setUserId(sysUser.getUserId());
			acStatisticsArticle.setUserName(sysUser.getUsername());
			QueryWrapper<AcArticleCount> qw1 = new QueryWrapper<AcArticleCount>();
			qw1.lambda().eq(AcArticleCount::getArticleId, statisticsArticlePO.getArticleId());
			AcArticleCount acArticleCount = acArticleCountService.getOne(qw1);
			if(acArticleCount == null) {
				acArticleCount = new AcArticleCount();
				acArticleCount.setArticleId(statisticsArticlePO.getArticleId());
				acArticleCount.setArticleTitle(statisticsArticlePO.getArticleTitle());
				acArticleCount.setCategoryName(statisticsArticlePO.getCategoryName());
				acArticleCount.setHitCount(1);
			}else {
				acArticleCount.setHitCount(acArticleCount.getHitCount()+1);
			}
			acArticleCountService.saveOrUpdate(acArticleCount);
		}
		acStatisticsArticleService.save(acStatisticsArticle);
		result.setData(true);
		return result;
	}
	
	
}
